import { Vec3 } from 'cc';
import { BaseState } from './BaseState';

/**
 * AI攻击状态
 * 
 * 功能：
 * 1. 处理攻击行为
 * 2. 管理攻击冷却
 * 3. 控制攻击动画
 * 4. 处理攻击效果
 * 
 * 特点：
 * 1. 多种攻击模式
 * 2. 攻击优先级
 * 3. 连击系统
 * 4. 攻击范围检测
 */
export class AttackState extends BaseState {
    private attackRange: number = 2;
    private attackCooldown: number = 1;
    private attackTimer: number = 0;
    private attackDuration: number = 0.5;
    private currentAttackTime: number = 0;

    public enter(): void {
        this.attackTimer = this.attackCooldown;
        this.performAttack();
    }

    public exit(): void {
        // 清理攻击状态
    }

    public update(dt: number): void {
        const target = this.controller.getTarget();
        if (!target) {
            this.controller.changeState('idle');
            return;
        }

        // 更新攻击计时器
        if (this.currentAttackTime > 0) {
            this.currentAttackTime -= dt;
            return;
        }

        // 更新冷却时间
        this.attackTimer -= dt;
        if (this.attackTimer <= 0) {
            const distance = Vec3.distance(this.controller.getPosition(), target.position);
            
            if (distance <= this.attackRange) {
                this.performAttack();
            } else {
                this.controller.changeState('chase');
            }
        }
    }

    private performAttack(): void {
        const target = this.controller.getTarget();
        if (!target) return;

        // 重置计时器
        this.attackTimer = this.attackCooldown;
        this.currentAttackTime = this.attackDuration;

        // 执行攻击
        this.controller.attack();
    }
} 